package com.imooc.config.service;

import com.imooc.config.dto.TaskDto;
import org.activiti.engine.*;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.task.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2018/12/3
 */
@Service
public class ActivitiServiceImpl implements ActivitiService {

    @Autowired
    private ProcessEngine processEngine;

    @Autowired
    private RuntimeService runtimeService;

    @Autowired
    private RepositoryService repositoryService;

    @Autowired
    private TaskService taskService;

    @Override
    public Object startProcess() {
        return runtimeService.startProcessInstanceByKey("demo_test");
    }

    @Transactional
    @Override
    public Object taskList() {
//        runtimeService.trigger("123");

//        List<ProcessDefinition> processDefinitionList = repositoryService.createProcessDefinitionQuery().list();
//        return processDefinitionList;

        List<Task> taskList = taskService.createTaskQuery().list();
        List<TaskDto> taskDtoList = new ArrayList<TaskDto>();
        for (Task task : taskList) {
            TaskDto taskDto = new TaskDto();
            taskDto.setAssignee(task.getAssignee());
            taskDto.setName(task.getName());
            taskDto.setTaskDefinitionKey(task.getTaskDefinitionKey());
            taskDtoList.add(taskDto);
        }

        return taskDtoList;
    }
}
